<template>
	<view>
		<view class="search-box">
			<uni-search-bar :radius="100" @input="input" :value="kw">
			</uni-search-bar>
		</view>
		
		<!-- 搜索建议列表 -->
		<!-- <view class="sugg-list" v-if="searchResults.length!==0">
			<view class="sugg-item" v-for="(item, i) in searchResults" :key="i" @click="gotoDetail(item)">
				<view class="goods-name">{{item.teamName}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view> -->
    <uni-section title="搜索结果" type="line" v-if="searchResults.length!==0" class="box">
    			<uni-card  v-for="(item, i) in searchResults" :key="i" @click="gotoDetail(item)"  :title="item.teamName" :sub-title="`创建人:${item.createBy}`":thumbnail="item.imgUrl">
            <text>说明：{{item.teamInfo}}</text>
    			</uni-card>
    </uni-section>
		
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
			</view>
			<!-- 列表区域 -->
			<view class="history-list">
				<uni-tag :text="item" :inverted="true" v-for="(item, i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
      import { selectServerBykey} from "@/api/team/team"
	export default {
		data() {
			return {
				timer:null,
				kw:'',
				// 搜索的结果列表
				searchResults:[],
				// 搜索历史的数组
				historyList:[]
			}
		},
		methods:{
			// input输入事件
			input(e){
				clearTimeout(this.timer)
				this.timer = setTimeout(()=>{
					this.kw=e
					this.getSearchList()
				},500)
			},
			async getSearchList(){
				// 判断搜索关键词是否为空
				if(this.kw.length===0){
					this.searchResults=[]
					return
				}
			
        // 发送请求获取数据
				selectServerBykey(this.kw).then(response=>{
          // console.log(data)
          this.searchResults=response.data
        })
				this.saveSearchHistory()
			},
			gotoDetail(team){
        console.log(team)
				uni.navigateTo({
					url: '/pages/mine/org-info/org-info?teamid='+team.teamId+'&type='+true,
				})
			},
			// 保存并添加搜索历史
			saveSearchHistory(){
				// this.historyList.push(this.kw)
				
				const set =new Set(this.historyList)
				
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList=Array.from(set)
				
				// 调用uni.setStorageSync(key,value) 将搜索历史记录持久化存储到本地
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			//清除搜索历史
			 cleanHistory(){
				 this.historyList=[]
				 uni.setStorageSync('kw','[]')
			 },
			 // 点击搜索历史跳转商品列表界面
			 gotoGoodsList(item){
				 // console.log(item);
				 this.kw=item
         console.log(this.kw)
         this.getSearchList(this.kw)
			 }
		},
		computed:{
			histories(){
				// 注意：由于数组是引用类型，所以不要直接基于原数组调用reverse方法，以免修改原数组中元素的顺序
				// 而是应该新建一个与内存无关的数组，在进行reverse反转
				return [...this.historyList].reverse()
			}
		},
		onLoad(){
			this.historyList=JSON.parse(uni.getStorageSync('kw')||'[]')
		}
	}
</script>

<style lang="scss">
page{
    // background: url('../../../static/images/dj5.png')  no-repeat center;
    // background-size: 450px;
    background-color: #ff2121;
  }
  .box{
        background-color: #ff2121;
  }
.search-box{
	position: sticky;
	top: 0;
	z-index: 999;
}
.sugg-list{
	padding: 0 5px;
	.sugg-item{
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 12px;
		padding: 13px 0;
		border-bottom: 1px solid #efefef;
		
		.goods-name{
			// 不允许换行
			white-space: nowrap;
			// 超出部分隐藏
			overflow: hidden;
			// 超出的部分用...表示
			text-overflow: ellipsis;
		}
	}
}

.history-box{
	padding: 0 5px;
	
	.history-title{
		display: flex;
		justify-content: space-between;
		height: 40px;
		align-items: center;
		font-size: 13px;
		border-bottom: 1px solid #efefef;
	}
	
	.history-list{
		display: flex;
		justify-content: flex-start;
		
		.uni-tag{
			margin-top: 5px;
			margin-right: 5px;
		}
	}
}
</style>
